package top.tangyh.lamp.care.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.tangyh.basic.exception.BizException;
import top.tangyh.lamp.care.entity.BEquipmentBrand;
import top.tangyh.lamp.care.mapper.BDevBrandTypeMapper;
import top.tangyh.lamp.care.service.BDevBrandTypeService;
import top.tangyh.basic.base.service.impl.SuperServiceImpl;
import top.tangyh.lamp.care.manager.BDevBrandTypeManager;
import top.tangyh.lamp.care.entity.BDevBrandType;
import top.tangyh.lamp.care.service.BEquipmentBrandService;
import top.tangyh.lamp.care.vo.query.DevBrandTypeSearchVo;
import top.tangyh.lamp.care.vo.result.DevBrandTypeDTO;
import top.tangyh.lamp.care.vo.result.EquipmentBrandDto;
import top.tangyh.lamp.care.vo.result.PageOutputDTO;
import top.tangyh.lamp.care.vo.save.BDevBrandTypeSaveVO;
import top.tangyh.lamp.care.vo.update.BDevBrandTypeUpdateVO;
import top.tangyh.lamp.care.vo.result.BDevBrandTypeResultVO;
import top.tangyh.lamp.care.vo.query.BDevBrandTypePageQuery;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 业务实现类
 * 设备品牌型号表
 * </p>
 *
 * @author zqb
 * @date 2024-07-15 16:42:41
 * @create [2024-07-15 16:42:41] [zqb] [代码生成器生成]
 */
@Slf4j
@RequiredArgsConstructor
@Service
@Transactional(readOnly = true)
public class BDevBrandTypeServiceImpl extends SuperServiceImpl<BDevBrandTypeManager, Long, BDevBrandType> implements BDevBrandTypeService {

    private final BDevBrandTypeMapper bDevBrandTypeMapper;

    private final BEquipmentBrandService bEquipmentBrandService;


    @Override
    public PageOutputDTO<DevBrandTypeDTO> queryPage(DevBrandTypeSearchVo query) {
        PageHelper.startPage(query.getPage(), query.getSize());
        QueryWrapper<BDevBrandType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_deleted", 0);
        queryWrapper.like(StringUtils.isNotBlank(query.getBrandCode()), "brand_code", query.getBrandCode());
        queryWrapper.like(StringUtils.isNotBlank(query.getBrandName()), "brand_name", query.getBrandName());
        List<BDevBrandType> list = bDevBrandTypeMapper.selectList(queryWrapper);
        PageInfo pageInfo = new PageInfo<>(list);
        return new PageOutputDTO<>(pageInfo.getTotal(), (long) pageInfo.getPages(), (long) pageInfo.getPageNum(),
                BeanUtil.copyToList(pageInfo.getList(), DevBrandTypeDTO.class)
        );
        // ConvertUtil.convertList(pageInfo.getList(), YxDevBrandTypeDTO.class)
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveBrandAndBrandType(EquipmentBrandDto dto) {
        BEquipmentBrand po = BeanUtil.toBean(dto, BEquipmentBrand.class);
        //ConvertUtil.convert(dto, YxEquipmentBrandPO.class);
        int count = bEquipmentBrandService.countOfEquipmentBrandCode(0, dto.getBrandCode(), null);
        if (count > 0) {
            throw new BizException("品牌标识不能重复！");
        }
        Long id = bEquipmentBrandService.saveEquipmentBrand(po);
        List<BDevBrandType> listBrandType = dto.getListBrandType();
        listBrandType.forEach(p -> p.setBrandId(id));

        for (int i = 0; i < listBrandType.size(); i++) {
            BDevBrandType devBrandTypePO = listBrandType.get(i);
            int counts = countOfBrandTypeCode(0, devBrandTypePO.getTypeCode(), null);
            if (counts > 0) {
                throw new BizException("设备型号标识不能重复！");
            }
        }
        return this.saveBatch(listBrandType);
    }

    @Override
    public int countOfBrandTypeCode(int saveOrUpdate, String typeCode, Long id) {
        QueryWrapper<BDevBrandType> queryWrapper = new QueryWrapper();
        if (saveOrUpdate == 0) {
            queryWrapper.eq("type_code", typeCode);
            queryWrapper.eq("is_deleted", 0);
        } else {
            queryWrapper.eq("is_deleted", 0);
            queryWrapper.eq("type_code", typeCode);
            queryWrapper.ne("id", id);
        }

        Integer count = bDevBrandTypeMapper.selectCount(queryWrapper).intValue();
        return count;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateBrandAndBrandType(EquipmentBrandDto dto, BEquipmentBrand po) {
        int count = bEquipmentBrandService.countOfEquipmentBrandCode(1, dto.getBrandCode(), dto.getId());
        if (count > 0) {
            throw new BizException("品牌标识不能重复！");
        }
        bEquipmentBrandService.updateById(po);
        for (int i = 0; i < dto.getListBrandType().size(); i++) {
            BDevBrandType brandTypePO = dto.getListBrandType().get(i);
            if (null != brandTypePO.getId()) {
                int counts = countOfBrandTypeCode(1, brandTypePO.getTypeCode(), brandTypePO.getId());
                if (counts > 0) {
                    throw new BizException("设备型号标识不能重复！");
                }
                updateById(brandTypePO);
            } else {
                int counts = countOfBrandTypeCode(0, brandTypePO.getTypeCode(), null);
                if (counts > 0) {
                    throw new BizException("设备型号标识不能重复！");
                }

                this.save(brandTypePO);
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteByBrandId(Long brandId) {
        LambdaUpdateWrapper<BDevBrandType> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(BDevBrandType::getBrandId, brandId);
        int result=bDevBrandTypeMapper.delete(wrapper);
        return result;
    }

//    @Override
//    public int updateByIdList(List<Long> ids) {
//        List<BDevBrandType> idss=new ArrayList<>();
//
//        bDevBrandTypeMapper.updateById(idss);
//        return 0;
//    }
}


